package com.neuedu.autosale.sys.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neuedu.autosale.entity.Emp;
import com.neuedu.autosale.entity.Pager;
import com.neuedu.autosale.entity.Result;
import com.neuedu.autosale.sys.service.EmpService;

@Controller
public class EmpController {
	@Autowired
	private EmpService empService;

	@RequestMapping("emp/index")
	private String empIndex() {
		return "emp/emplist";
	}

	@RequestMapping("emp/findall")
	@ResponseBody
	public List<Emp> emplist() {
		return empService.findAll();
	}

	@RequestMapping("emp/list")
	@ResponseBody
	public Pager<Emp> empList(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "10") Integer rows,HttpServletRequest request) {
		String toName = request.getParameter("empName");
		Emp empName = new Emp(toName);
		Pager<Emp> pager = new Pager<Emp>(page, rows, empName);
//		Pager<Emp> pager = new  Pager<>(page, rows);
		List<Emp> empList = empService.findListByPager(pager);
		int total = empService.findTotalByPager(pager);
		pager.setRows(empList);
		pager.setTotal(total);
		return pager;
	}

	@RequestMapping("emp/add")
	@ResponseBody
	public Result doAdd(Emp emp) {
		Result result = empService.add(emp);
		return result;
	}

	@RequestMapping("emp/edit")
	@ResponseBody
	public Result doEdit(Emp emp) {
		Result result = empService.edit(emp);
		return result;
	}

	@RequestMapping("emp/delete")
	@ResponseBody
	public Result doDelete(Integer empId) {
		List<Emp> list = new ArrayList<Emp>();
		list.add(new Emp(empId));
		Result result = empService.deleteByIds(list);
		return result;
	}

}
